[SQL Server]从字符串转换为 datetime 时发生语法错误

来源:百度知道 编辑:UC知道 时间:2024/06/15 09:50:21
sql="select count(id) as ones from [data] where emdate>'2009-01-01' and CONVERT(VarChar(100),arrivetime,23)>='"×&"' and CONVERT(VarChar(100),arrivetime,23)<='"×s&"' and station like '%"&zone&"' and provider like '%"&provide&plu&"'and poster like '%"&poster&"' and leadtime is not null and station<>'库房' and backtime is not null and DATEADD(day,1,CONVERT(VarChar(100),backtime,23))<=arrivetime "

数据库中 backtime 字段为字符串格式,arrivetime 是datetime 格式
请求给与正确的语句,
筛选 backtime 和 arrivetime 相差小于等于一天的数据集

运行提示
Microsoft OLE DB Provider for ODBC Drivers 错误 '80040e07'

[Microsoft][ODBC SQL Server Driver][SQL Server]从字符串转换为 datetime 时发生语法错误。
sql="select count(id) as ones from [data] where emdate>'2009-01-01' backtime is not null and DATEADD(day,1,CONVERT(VarChar(100),backtime,23

挺简单的你写的这么复杂!
看了半天才看懂!
你用 Substring 提取字符串!
把天数提取出来!
在用 select datepart(dd,参数)
把两个提取出来在比较不就玩了!
我建议你写成函数真的!
调用函数就行了!
你这的写也不便与维修!很麻烦的

使用datediff吧!
datediff(d,backtime,arrivetime)<=1 (筛选 backtime 比 arrivetime 小于等于一天的数据集) 若还要筛选 arrivetime 比 backtime 小于等于一天的数据集 则还要家 or datediff(d,arrivetimee,backtim)<=1
哪有你那么麻烦!